Is there a way to use template specialization to separate new from new[]?
Posted
by Marlon
on Stack Overflow
See other posts from Stack Overflow
or by Marlon
Published on 2010-04-07T08:19:43Z
Indexed on
2010/04/07
8:23 UTC
Read the original article
Hit count: 260
I have an auto pointer class and in the constructor I am passing in a pointer. I want to be able to separate new from new[] in the constructor so that I can properly call delete or delete[] in the destructor. Can this be done through template specialization? I don't want to have to pass in a boolean in the constructor.
template <typename T>
class MyAutoPtr
{
public:
MyAutoPtr(T* aPtr);
};
// in use:
MyAutoPtr<int> ptr(new int);
MyAutoPtr<int> ptr2(new int[10]);
© Stack Overflow or respective owner